//
//  DSZMainViewController.m
//  7.13苏宁框架
//
//  Created by lovena on 15/7/13.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZMainViewController.h"
#import "DSZTabbarView.h"
#import "DSZShowViewController.h"
#import "Common.h"
#import "ShopModel.h"
@interface DSZMainViewController ()

@property(nonatomic,strong)UIButton *btn;
@end


@implementation DSZMainViewController

-(void)loadView
{
    [super loadView];
    [self initSubTabbars];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    //造一个按钮
    _btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [_btn setBackgroundColor:[UIColor redColor]];

    _btn.titleLabel.font=[UIFont systemFontOfSize:10];
    _btn.enabled=NO;
    _btn.frame=CGRectMake(230, 4, 14, 14);
    _btn.layer.cornerRadius=7;
    _btn.layer.masksToBounds=YES;
    [self.tabbarView addSubview:_btn];
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView) name:@"pushFen" object:nil];
    
    //隐藏分栏
    self.tabBar.hidden=YES;
}
-(void)viewWillAppear:(BOOL)animated
{
    [self loadBadgeInShop];
}

-(void)initSubTabbars
{
    //位置大小
    _tabbarView=[[DSZTabbarView alloc]initWithFrame:CGRectZero];
    _tabbarView.frame=CGRectMake(0, 480-49, 320, 49);
    _tabbarView.delegate=self;
    [self.view addSubview:_tabbarView];
}
//代理
-(void)DSZButtonTag:(NSInteger)index
{

    self.selectedIndex=index;
    if(index==2)
    {
    //[self initSubTabbars];
        //跳入show
    [self hiddenMainTabbar:YES];
        UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        DSZShowViewController *vc=[story instantiateViewControllerWithIdentifier:@"show"];
        [self presentViewController:vc animated:NO completion:nil];
       
    }
}
//-(void)changeView
//{
//    [self DSZButtonTag:1];
//}
-(void)hiddenMainTabbar:(BOOL)flag
{
    if (flag==YES) {

        _tabbarView.frame=CGRectMake(0, 480, 320, 49);
    }
    else
    {
      _tabbarView.frame=CGRectMake(0, 480-49, 320, 49);
    }
}
-(void)chooseSection:(NSInteger)index
{
    //选择第几个模块
    self.selectedIndex=index;
}
//分栏显示徽章
-(void)loadBadgeView:(NSInteger)index
{

   [_btn setTitle:[NSString stringWithFormat:@"%i",index] forState:UIControlStateNormal];
    if (index<=0) {
    _btn.hidden=YES;
    }
    else
    {
        _btn.hidden=NO;
    }
}


//得出购物车有多少订单
-(void)loadBadgeInShop
{
    NSMutableArray *array=[NSMutableArray array];
    //取出
    array=[NSKeyedUnarchiver unarchiveObjectWithFile:FilePath];
   //求徽章数
    int count=0;
    for (ShopModel *shopmodel in array) {
        count+=shopmodel.count;
    }
    
        [self loadBadgeView:count];

}
@end
